package com.gxa.admin.controller;

import com.gxa.admin.pojo.dto.VipDto;
import com.gxa.admin.service.VipService;
import com.gxa.admin.utils.QueryResult;
import com.gxa.admin.utils.ValidatorUtils;
import com.gxa.core.result.MessageCode;
import com.gxa.core.result.Result;
import com.gxa.core.result.ResultUtils;
import com.gxa.pojo.Vip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lch
 * @date 2024/3/20 15:17
 * @description 会员等级
 **/
@RestController
public class VipController {

    @Autowired
    private VipService vipService;

    /**
     * 查询所有会员等级
     * @param vipDto
     * @return
     */
    @GetMapping("/admin/vipLevel/selectVipLevelByPage")
    public Result getAllVipLevel(VipDto vipDto) {
        QueryResult queryResult = this.vipService.getAllVipLevel(vipDto);
        if (queryResult != null) {
            //查询成功
            return ResultUtils.buildSuccess(queryResult.getCount(), queryResult.getData());
        } else {
            //查询失败
            return ResultUtils.buildFail(MessageCode.UN_KNOW_EXCEPTION);
        }
    }

    /**
     * 添加会员等级
     * @param vipDto
     * @return
     */
    @PostMapping("/admin/vipLevel/addVipLevel")
    public Result addVipLevel(@RequestBody VipDto vipDto) {

        //数据校验
        ValidatorUtils.validateEntity(vipDto);

        int result = this.vipService.addVipLevel(vipDto);
        if (result > 0) {
            //添加成功
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        } else {
            //添加失败
            return ResultUtils.buildFail(MessageCode.UN_KNOW_EXCEPTION);
        }
    }


    /**
     * 更新会员等级
     * @param vipDto
     * @return
     */
    @PostMapping("/admin/vipLevel/updateVipLevel")
    public Result updateVipLevel(@RequestBody VipDto vipDto) {

        //数据校验
        ValidatorUtils.validateEntity(vipDto);

        int result = this.vipService.updateVipLevel(vipDto);
        if (result > 0) {
            //更新成功
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        } else {
            //更新失败
            return ResultUtils.buildFail(MessageCode.UN_KNOW_EXCEPTION);
        }
    }


    /**
     * 根据id删除会员等级
     * @param vipId
     * @return
     */
    @PostMapping("/admin/vipLevel/deleteVipLevel")
    public Result deleteVipLevel(Integer vipId){
        int result = this.vipService.deleteVipLevel(vipId);
        if (result > 0) {
            //删除成功
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        } else {
            //删除失败
            return ResultUtils.buildFail(MessageCode.UN_KNOW_EXCEPTION);
        }
    }


    /**
     * 根据用户id查询会员等级
     * @param vipId
     * @return
     */
    @GetMapping("/admin/vipLevel/selectVipLevelById")
    public Result getUserById(Integer vipId){
        Vip vip = this.vipService.getVipById(vipId);
        if (vip!=null){
            //查询成功
            return ResultUtils.buildSuccess(vip);
        }else {
            //查询失败
            return ResultUtils.buildFail(MessageCode.UN_KNOW_EXCEPTION);
        }
    }

}
